package com.ccp.dev.extend.model;

import com.ccp.dev.core.util.JSONObjectUtil;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.system.model.ColumnInfo;
import com.ccp.dev.system.model.TableInfo;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: dfz
 * @Date: 2019/5/20 09:28
 * @Description:模板基本信息
 */
public class ExtendBaseModuleInfo {

    /**
     * 表名称
     */
    private String tableName ;

    /**
     * 表的描述说明信息
     */
    private String tableComment;

    /**
     * 表的主键信息
     */
    private ExtendColumnInfo primaryKey;

    /**
     *  类名(第一个字母大写)
     */
    private String classForClassName;


    /**
     *  类名(第一个字母小写)
     */
    private String classForPrefix;

    /**
     * 模块名称
     */
    private String moduleName;

    /**
     * 模块访问URL的根路径
     *  例如demo模块的页面入口位置URL /demo
     */
    private  String moduleViewName;

    /**
     * 不包含主键列的其他列信息
     */
    private List<ExtendColumnInfo> columns ;

    /**
     * 基本的包路径地址
     */
    private String basePackage;

    /**
     * 文件所在的文件夹名称
     *  例如：TestDemoService类在service文件夹下
     */
    private String filePackage;

    /**
     * 生成的zip文件的文件名称
     */
    private String fileName;

    /**
     * 生成的代码注释中的作者信息
     */
    private String  author;

    /**
     * 代码生成时间
     */
    private String datetime;

    private Boolean isShowFull;

    private Boolean selectOpenMode;

    private String selectColumn;

    /**
     * 隐藏列
     */
    private List<ExtendColumnInfo> hideColumns ;

    /**
     * 显示列
     */
    private List<ExtendColumnInfo> showColumns ;

    public String getClassForClassName() {
        return classForClassName;
    }

    public void setClassForClassName(String classForClassName) {
        this.classForClassName = classForClassName;
    }

    public String getClassForPrefix() {
        return classForPrefix;
    }

    public void setClassForPrefix(String classForPrefix) {
        this.classForPrefix = classForPrefix;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = replaceKeyword(tableComment);
    }

    public ExtendColumnInfo getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(ExtendColumnInfo primaryKey) {
        this.primaryKey = primaryKey;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getModuleViewName() {
        return moduleViewName;
    }

    public void setModuleViewName(String moduleViewName) {
        this.moduleViewName = moduleViewName;
    }

    public List<ExtendColumnInfo> getColumns() {
        return columns;
    }

    public void setColumns(List<ExtendColumnInfo> columns) {
        this.columns = columns;
    }

    public String getBasePackage() {
        return basePackage;
    }

    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public String getFilePackage() {
        return filePackage;
    }

    public void setFilePackage(String filePackage) {
        this.filePackage = filePackage;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    public Boolean getShowFull() {
        return isShowFull;
    }

    public void setShowFull(Boolean showFull) {
        isShowFull = showFull;
    }

    public Boolean getSelectOpenMode() {
        return selectOpenMode;
    }

    public void setSelectOpenMode(Boolean selectOpenMode) {
        this.selectOpenMode = selectOpenMode;
    }

    public String getSelectColumn() {
        return selectColumn;
    }

    public void setSelectColumn(String selectColumn) {
        this.selectColumn = selectColumn;
    }

    public List<ExtendColumnInfo> getHideColumns() {
        return hideColumns;
    }

    public void setHideColumns(List<ExtendColumnInfo> hideColumns) {
        this.hideColumns = hideColumns;
    }

    public List<ExtendColumnInfo> getShowColumns() {
        return showColumns;
    }

    public void setShowColumns(List<ExtendColumnInfo> showColumns) {
        this.showColumns = showColumns;
    }

    public ExtendBaseModuleInfo(){}

    public ExtendBaseModuleInfo(ExtendTableInfo table, List<ExtendColumnInfo> columns){
        this.tableName=table.getTableName();
        this.tableComment=replaceKeyword(table.getTableComment());
        this.primaryKey = table.getPrimaryKey();
        this.classForClassName = table.getClassForClassName();
        this.classForPrefix = table.getClassForPrefix();
        this.columns = columns;
        this.hideColumns = hideColumns;
        this.showColumns = showColumns;
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.datetime = dataFormat.format(new Date());
    }



    /**
     *
     * 功能描述: 根据表信息、列信息和JSON资源信息封装模板基础对象
     *
     * @param: table 表信息对象
     * @param: columns  列信息对象
     * @param: resource  JSON资源对象
     * @auther: dfz
     * @date: 2019/5/20 14:10
     */
    public ExtendBaseModuleInfo(ExtendTableInfo table, List<ExtendColumnInfo> columns, Resource resource){

        this.tableName=table.getTableName();
        this.tableComment=replaceKeyword(table.getTableComment());
        this.primaryKey = table.getPrimaryKey();
        this.classForClassName = table.getClassForClassName();
        this.classForPrefix = table.getClassForPrefix();
        this.columns = columns;
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.datetime = dataFormat.format(new Date());

        //读取配置文件封装到baseModuleInfo中，在模板文件中读取相应属性
        StringBuffer sb = new StringBuffer();
        try{
            File file = resource.getFile();
            Scanner scanner = new Scanner(file, "utf-8");
            while (scanner.hasNextLine()) {
                sb.append(scanner.nextLine());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        HashMap<String,String> jsonMap = JSONObjectUtil.toBean(sb.toString(), HashMap.class);
        this.moduleName=jsonMap.get("moduleName");
        this.moduleViewName=jsonMap.get("moduleViewName");
        this.basePackage=jsonMap.get("basePackage");
        this.filePackage=jsonMap.get("filePackage");
        this.author=jsonMap.get("author");
    }

    public ExtendBaseModuleInfo(HttpServletRequest request){
        this.tableName = RequestUtil.getString(request,"tableName");
        this.basePackage = RequestUtil.getString(request,"basePackage");
        this.moduleName = RequestUtil.getString(request,"moduleName");
        this.classForClassName = RequestUtil.getString(request,"classForClassName");
        this.fileName = RequestUtil.getString(request,"fileName");
        this.author = RequestUtil.getString(request,"author");

    }


    /**
     * 替换注释信息里面的关键字
     * */
    private String replaceKeyword(String keyword) {
        String keyName = keyword.replaceAll("(?:表|信息)", "");
        return keyName;
    }

}
